package com.fafian.gdc.buttons;

import com.fafian.gdc.core.BusCommand;
import com.fafian.gdc.core.Command;
import com.fafian.gdc.core.Source;
import com.fafian.publish.PublishBus;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

public class ButtonsController implements GpioPinListenerDigital {

	private int buttonPin1, buttonPin2;
	private PublishBus bus;
	
	public ButtonsController(PublishBus bus, int buttonPin1,
			int buttonPin2){
		this.bus = bus;
		this.buttonPin1 = buttonPin1;
		this.buttonPin2 = buttonPin2;
		
	}
	
	@Override
	public void handleGpioPinDigitalStateChangeEvent(
			GpioPinDigitalStateChangeEvent arg0) {
		
		if (buttonPin1 == arg0.getPin().getPin().getAddress() && (arg0.getState() == PinState.HIGH)){
			if (arg0.getState() == PinState.HIGH){
				System.out.println("Physical Button 1 touched");
				BusCommand bc = new BusCommand(Command.PUSH_BUTTON1,
						Source.GPIO);
				bus.publish(bc);
			}
		}
		
		if (buttonPin2 == arg0.getPin().getPin().getAddress() && (arg0.getState() == PinState.HIGH)){
			if (arg0.getState() == PinState.HIGH){
				System.out.println("Physical Button 2 touched");
				BusCommand bc = new BusCommand(Command.PUSH_BUTTON2,
						Source.GPIO);
				bus.publish(bc);
			}
		}
			
		
	}

}
